package com.lam.mall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.lam.common.base.page.PageVo;
import com.lam.common.base.page.TableHelper;
import com.lam.common.utils.StringUtils;
import com.lam.mall.entity.ProdSpecs;
import com.lam.mall.entity.ProdSpecsValue;
import com.lam.mall.mapper.ProdSpecsMapper;
import com.lam.mall.mapper.ProdSpecsValueMapper;
import com.lam.mall.param.ProdSpecsParam;
import com.lam.mall.service.ProdSpecsService;

/**
 * 规格管理Service业务层处理
 * 
 * @author admin
 * @date 2023-04-21
 */
@Service
public class ProdSpecsServiceImpl  implements ProdSpecsService {

    @Autowired
    private ProdSpecsMapper prodSpecsMapper;
    @Autowired
    private ProdSpecsValueMapper prodSpecsValueMapper;

    /**
     * 查询规格管理
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public ProdSpecs selectById(String id) {

        return prodSpecsMapper.selectById(id);
    }

    @Override
    public List<ProdSpecs> listSpecsAndValue(ProdSpecsParam param) {

    	PageVo pageVo = TableHelper.getPageVo();
    	Page<ProdSpecs> page = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());
    	page.setTotal(prodSpecsMapper.countSpecsAndValue(param));
    	if(page.getTotal() > 0) {
    		page.addAll(prodSpecsMapper.listSpecsAndValue(param, pageVo));
    	}
        return page;
    }
    
    /**
     * 查询规格管理列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<ProdSpecs> selectList(ProdSpecsParam param) {
    
        QueryWrapper<ProdSpecs> queryWrapper = new QueryWrapper<>();

        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
            queryWrapper.and(wr->{
                wr.or().like("specs_name", param.getKeywords());
            });
        }
        
        queryWrapper.like(StringUtils.isNotBlank(param.getSpecsNameKey()), "specs_name", param.getSpecsNameKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getSpecsName()), "specs_name", param.getSpecsName());
        
        queryWrapper.eq(StringUtils.isNotBlank(param.getShopId()), "shop_id", param.getShopId());

        return prodSpecsMapper.selectList(queryWrapper);
    }

    /**
     * 新增规格管理
     * 
     * @param prodSpecs 
     * @return 
     */
    @Override
    @Transactional
    public int insert(ProdSpecs prodSpecs) {
    
    	int n = prodSpecsMapper.insert(prodSpecs);
    	List<ProdSpecsValue> list = prodSpecs.getProdSpecsValues();
    	for (ProdSpecsValue specsValue : list) {
    		specsValue.setSpecsId(prodSpecs.getId());
    		prodSpecsValueMapper.insert(specsValue);
		}
        return n;
    }

    /**
     * 修改规格管理
     * 
     * @param prodSpecs 
     * @return 
     */
    @Override
    @Transactional
    public int update(ProdSpecs prodSpecs) {
    
    	prodSpecsMapper.updateById(prodSpecs);
    	List<ProdSpecsValue> list = prodSpecs.getProdSpecsValues();
    	for (ProdSpecsValue specsValue : list) {
    		specsValue.setSpecsId(prodSpecs.getId());
    		if(StringUtils.isBlank(specsValue.getId())) {
    			prodSpecsValueMapper.insert(specsValue);
    		}else {
    			prodSpecsValueMapper.updateById(specsValue);
    		}
		}
        return 1;
    }

    /**
     * 删除规格管理信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    
        if(StringUtils.isBlank(idStr)) {
            return 0;
        }
        
        List<String> idList = StringUtils.str2List(idStr);
        for (String id : idList) {
        	prodSpecsMapper.deleteById(id);
        	QueryWrapper<ProdSpecsValue> queryWrapper = new QueryWrapper<>();
        	queryWrapper.eq("specs_id", id);
        	prodSpecsValueMapper.delete(queryWrapper);
		}
        return 1;
    }
    
    
}
